package com.grj.parkingsystem.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.grj.parkingsystem.common.Result;
import com.grj.parkingsystem.service.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vehicle")
@Api(tags = "2. 车辆管理")
public class VehicleController {

    @Autowired
    private VehicleService vehicleService;

    @PostMapping("/bind")
    @ApiOperation(value = "绑定车辆", notes = "用户绑定车辆信息")
    @ApiOperationSupport(order = 1)
    public Result<?> bindVehicle(@ApiParam(value = "认证Token", required = true)
                                @RequestHeader("Authorization") String token,
                                @RequestParam String licensePlate,
                                @RequestParam String vehicleType,
                                @RequestParam String color) {
        boolean b = vehicleService.bindVehicle(token, licensePlate, vehicleType, color);
        if (!b){
            return Result.error("绑定失败");
        }
        return Result.success("绑定成功");
    }

    @PostMapping("/unbind/{vehicleId}")
    @ApiOperation(value = "解绑车辆", notes = "用户解绑已绑定的车辆")
    @ApiOperationSupport(order = 2)
    public Result<?> unbindVehicle(@ApiParam(value = "认证Token", required = true)
                                 @RequestHeader("Authorization") String token,
                                 @PathVariable Long vehicleId) {
        vehicleService.unbindVehicle(token, vehicleId);
        return Result.success("解绑成功");
    }

    @PostMapping("/setDefault/{vehicleId}")
    @ApiOperation(value = "设置默认车辆", notes = "设置某辆车为用户的默认车辆")
    @ApiOperationSupport(order = 3)
    public Result<?> setDefaultVehicle(@ApiParam(value = "认证Token", required = true)
                                     @RequestHeader("Authorization") String token,
                                     @PathVariable Long vehicleId) {
        vehicleService.setDefaultVehicle(token, vehicleId);
        return Result.success("设置成功");
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取车辆列表", notes = "获取当前用户绑定的所有车辆")
    @ApiOperationSupport(order = 4)
    public Result<?> listVehicles(@ApiParam(value = "认证Token", required = true)
                                @RequestHeader("Authorization") String token) {
        return Result.success(vehicleService.getVehicles(token));
    }
}